﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using System.Windows.Forms;
using Google.GData.Extensions;
using Google.GData.Client;
using Google.GData.Contacts;
using Google.GData.Calendar;
using Google.Contacts;

namespace GoogleAccountAddIn
{
    public partial class ThisAddIn
    {
        private string username;
        private string password;

        private Office.CommandBar menuBar;
        private Office.CommandBarPopup newMenuBar;
        private Office.CommandBarButton buttonConfig;
        private Office.CommandBarButton buttonSync;
        private string menuTag = "GoogleAccountAddIn";

        private static Configuration configForm;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            RemoveMenuBar();
            AddMenuBar();
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        private void AddMenuBar()
        {
            try
            {
                menuBar = this.Application.ActiveExplorer().CommandBars.ActiveMenuBar;
                newMenuBar = (Office.CommandBarPopup)menuBar.Controls.Add(
                    Office.MsoControlType.msoControlPopup, missing,
                    missing, missing, false);
                if (newMenuBar != null)
                {
                    newMenuBar.Caption = "Google Account";
                    newMenuBar.Tag = menuTag;

                    buttonConfig = (Office.CommandBarButton)newMenuBar.Controls.Add(
                        Office.MsoControlType.msoControlButton, missing,
                        missing, 1, true);
                    buttonConfig.Style = Office.MsoButtonStyle.msoButtonCaption;
                    buttonConfig.Caption = "Configuration";
                    buttonConfig.Tag = "Config";
                    buttonConfig.Click += new Office._CommandBarButtonEvents_ClickEventHandler(buttonConfig_Click);

                    buttonSync = (Office.CommandBarButton)newMenuBar.Controls.Add(
                        Office.MsoControlType.msoControlButton, missing,
                        missing, 2, true);
                    buttonSync.Style = Office.MsoButtonStyle.msoButtonCaption;
                    buttonSync.Caption = "Synchronise";
                    buttonSync.Tag = "Sync";
                    buttonSync.Click += new Office._CommandBarButtonEvents_ClickEventHandler(buttonSync_Click);

                    newMenuBar.Visible = true;
                }
            }
            catch (Exception ex)
            {
                ErrorHandler(ex);
            }
        }

        void buttonSync_Click(Office.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            // get Outlook's contact list
            Outlook.Items outlookContacts = (this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts) as Outlook.Folder).Items;
                       
            // get google account contact list
            // testing account
            username = "leijiamin@gmail.com";
            password = "shljm791104";

            RequestSettings rs = new RequestSettings(this.Application.ToString(), this.username, this.password);
            rs.AutoPaging = true;
            ContactsRequest cr = new ContactsRequest(rs);

            Feed<Contact> googleContacts;
            googleContacts = cr.GetContacts();

            foreach (Contact c2 in googleContacts.Entries)
            {
                foreach (Outlook.ContactItem c1 in outlookContacts)
                {
                    if (!CompareTwoContact(c1, c2))
                    {
                        MessageBox.Show("FOUND");
                    }
                }
            }
        }

        void buttonConfig_Click(Office.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            if ((configForm == null) || (configForm.IsDisposed))
            {
                configForm = new Configuration();
            }

            configForm.Show();
            configForm.Activate();
        }

        private void RemoveMenuBar()
        {
            try
            {
                Office.CommandBarPopup foundMenu = (Office.CommandBarPopup)
                    this.Application.ActiveExplorer().CommandBars.ActiveMenuBar.FindControl(
                    Office.MsoControlType.msoControlPopup, missing, menuTag, true, true);
                if (foundMenu != null)
                {
                    foundMenu.Delete(true);
                }
            }
            catch (Exception ex)
            {
                ErrorHandler(ex);
            }
        }

        private void ErrorHandler(Exception ex)
        {
            MessageBox.Show("Error: " + ex.Message.ToString(), "Google Account Add-In Error Message",
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        private bool CompareTwoContact(Outlook.ContactItem c1, Contact c2)
        {
            return c2.Phonenumbers.Contains(new PhoneNumber(c1.HomeTelephoneNumber));
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }
}
